//
//  VisualViewController.m
//  礼物
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 liangsicheng. All rights reserved.
//

#import "VisualViewController.h"

@interface VisualViewController ()

@end

@implementation VisualViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //视图将要出现的时候刷新
    [self.visualView.myCollectionView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"视频";
    self.visualView=[[VisualView alloc]initWithFrame:MAINSCREEN];
    [self.view addSubview:self.visualView];
    self.visualView.delegate=self;
    [self createData];
    [self addHeaderRefresh];
    [self addFooterRefresh];
    
   }


- (void)createData{
    [AppToolss getAFNetWorkingData:@"http://baobab.wandoujia.com/api/v1/feed?num=1&udid=5614f5b294c8474a8252daad4f23ea3cbc5401e5&vc=51&vn=1.6.1&deviceModel=PE-TL10&first_channel=eyepetizer_zhihuiyun_market&last_channel=eyepetizer_zhihuiyun_market" andBlock:^(id data) {
        self.visualView.visualCellArray=[NSMutableArray array];
        NSFileManager *fileManager=[NSFileManager defaultManager];
        NSArray *fileArray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentPath=[fileArray lastObject];
        NSString *archivePath =[documentPath stringByAppendingPathComponent:@"visual"];
        BOOL relust1 =    [fileManager removeItemAtPath:archivePath error:nil];
        NSLog(@"==== %@",archivePath);
        if (relust1) {
            NSLog(@"沙盒文件删除成功");
        }else
        {
            NSLog(@"沙盒文件删除失败");
        }
        BOOL relust4 =  [NSKeyedArchiver archiveRootObject:data toFile:archivePath];
        if (relust4) {
            NSLog(@"归档成功");
        }else
        {
            NSLog(@"归档失败");
        }
        
        NSMutableDictionary *dictionary = data;
        self.nextPageUrl = [dictionary objectForKey:@"nextPageUrl"];
        NSMutableArray *dailyListArray = [dictionary objectForKey:@"dailyList"];
        NSMutableDictionary *dic = [dailyListArray objectAtIndex:0];
        NSMutableArray *videoListArray = [dic objectForKey:@"videoList"];
        for (NSMutableDictionary *dict in videoListArray)
        {
            VisualModel *videoModel = [[VisualModel alloc] init];
            [videoModel setValuesForKeysWithDictionary:dict];
            videoModel.descriptions = [dict objectForKey:@"description"];
            //给字典里的元素(收藏和喜欢)赋值
            [videoModel setValuesForKeysWithDictionary:videoModel.consumption];
            [self.visualView.visualCellArray addObject:videoModel];
        }
        [self.visualView.myCollectionView reloadData];
        [self.visualView.myCollectionView.header endRefreshing];
    } andError:^(id failure) {
    
        
    }];
}

- (void)addHeaderRefresh
{
    self.visualView.myCollectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self createData];
    }];
    
    [self.visualView.myCollectionView.header beginRefreshing];
}

//上拉
- (void)addFooterRefresh
{
    self.visualView.myCollectionView.header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [AppToolss getAFNetWorkingData:self.nextPageUrl andBlock:^(id data) {
            self.visualView.visualCellArray=[NSMutableArray array];
            NSFileManager *fileManager=[NSFileManager defaultManager];
            NSArray *fileArray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentPath=[fileArray lastObject];
            NSString *archivePath =[documentPath stringByAppendingPathComponent:@"visual"];
            BOOL relust1 =    [fileManager removeItemAtPath:archivePath error:nil];
            NSLog(@"==== %@",archivePath);
            if (relust1) {
                NSLog(@"沙盒文件删除成功");
            }else
            {
                NSLog(@"沙盒文件删除失败");
            }
            BOOL relust4 =  [NSKeyedArchiver archiveRootObject:data toFile:archivePath];
            if (relust4) {
                NSLog(@"归档成功");
            }else
            {
                NSLog(@"归档失败");
            }
            
            NSMutableDictionary *dictionary = data;
            self.nextPageUrl = [dictionary objectForKey:@"nextPageUrl"];
            NSMutableArray *dailyListArray = [dictionary objectForKey:@"dailyList"];
            NSMutableDictionary *dic = [dailyListArray objectAtIndex:0];
            NSMutableArray *videoListArray = [dic objectForKey:@"videoList"];
            for (NSMutableDictionary *dict in videoListArray)
            {
                VisualModel *videoModel = [[VisualModel alloc] init];
                [videoModel setValuesForKeysWithDictionary:dict];
                videoModel.descriptions = [dict objectForKey:@"description"];
                //给字典里的元素(收藏和喜欢)赋值
                [videoModel setValuesForKeysWithDictionary:videoModel.consumption];
                [self.visualView.visualCellArray addObject:videoModel];
            }
            [self.visualView.myCollectionView reloadData];
            [self.visualView.myCollectionView.footer endRefreshing];
        } andError:^(id failure) {}];
        
    }];
}




-(void)pushDetailController:(VisualModel *)model
{
    VisualDetailViewController *visualDetailVC=[[VisualDetailViewController alloc]init];
    visualDetailVC.model=model;
    [self.navigationController pushViewController:visualDetailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
